<template>
    <Card>
        <div>
            <Affix :offset-top="120">
                <div class="contract-detial-affix">
                    <div style="text-align: left;"
                         v-if="!isSP && this.params.editFlag && this.params.processStatus!=='draft' &&this.params.processStatus!=='toBeRepaired'">
                        <Button type="primary" @click="back()">返回</Button>
                        <Button type="primary" v-if="!readOnly" @click="approve('pass')">通过</Button>
                        <Button type="primary" v-if="!readOnly" @click="approve('return')">退回</Button>
                    </div>
                    <div style="text-align: left;"
                         v-else-if="!isSP && this.params.editFlag && this.params.processStatus!=='draft'">
                        <Button type="primary" @click="back()">返回</Button>
                        <Button type="primary" v-if="!readOnly" @click="approve('pass')">通过</Button>
                    </div>
                    <div style="text-align: right;" v-else>
                        <Button type="primary" @click="back()">返回</Button>
                        <!--       <Button type="primary" v-if="!readOnly" @click="save()">保存</Button>-->
                        <Button type="primary" v-if="!readOnly" @click="submit">提交</Button>
                    </div>
                </div>
            </Affix>

        </div>
        <Collapse v-model="openPanel">
            <Panel name="1" v-if="pageParams.processStatus != 'draft'">
                日志
                <div class="content" slot="content">
                    <approveHistory :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="2">
                基本信息
                <div class="content" slot="content">
                    <BaseInfo ref="baseInfo" :formData="pageInfo"/>
                </div>
            </Panel>
            <Panel name="3">
                合同信息
                <div class="content" slot="content">
                    <contractInfo ref="contractInfo" :Obj="Obj" :readOnly="readOnly"/>
                </div>
            </Panel>
            <Panel name="4">
                金融产品
                <div class="content" slot="content">
                    <FinanceInfo :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="5">
                撤销处理
                <div class="content" slot="content">
                    <revokeHandleInfo
                        ref="revokeHandleInfo"
                        :Obj="Obj"
                        :dataDict="dataDict"
                        :readOnly="readOnly"
                        :isApprove="isApprove"
                        :formData="revokeHandleInfo"
                        @closeSpin="closeSpin"
                        @submitDataFn="submitDataFn"
                        @childBusinessForm="childBusinessForm"
                    />
                </div>
            </Panel>
            <Panel name="6">
                收据信息
                <div class="content" slot="content">
                    <RevokeFrom
                        ref="RevokeFrom"
                        :formData="revokeFrom"
                        :Obj="Obj"
                        :dataDict="dataDict"
                    />
                </div>
            </Panel>
            <Panel name="7"
                   v-if="!readOnly && !isSP && params.editFlag && this.params.processStatus!=='draft' ">
                当前用户: {{userInfo ? userInfo.userRealName :''}}
                <div class="content" slot="content">
                    <ContractCurrentUser ref="contractCurrentUser" :Obj="{type:'revokeHandle'}" :needFile="needFile"
                                         @childUserForm="childUserForm" @submitDataFn="submitDataFn"
                                         @closeSpin="closeSpin"/>
                </div>
            </Panel>
        </Collapse>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </Card>
</template>
<script>
    import {mapActions, mapGetters} from "vuex";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import {businessSource, channelType, FinanceType} from "_p/basic/api/contract/channel-type/channel-type";
    import {
        revokedStartProcess,
        revokeSubmitProcess,
        getManageRevokeHandle
    } from "_p/basic/api/contract/revoke/revoke-handle";
    // 日志
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 业务办理
    import RevokeFrom from "../../revoke-collection/RevokeFrom";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    // 金融产品
    import FinanceInfo from "_p/basic/pages/contract/common/finance-info/finance-info";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info";
    import revokeHandleInfo from "./revokeHandleInfo.vue";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    import {getReceiptRecord} from "_p/basic/api/contract/revoke/revoke-collection";

    export default {
        name: "index",
        components: {
            approveHistory,
            contractInfo,
            BaseInfo,
            FinanceInfo,
            ContractCurrentUser,
            RevokeFrom,
            revokeHandleInfo
        },

        data() {
            return {
                spinShow: false,
                needFile: false,
                loading: true,
                openPanel: [2, 3, 4, 5, 6, 7],
                readOnly: null,
                isApprove: false,
                contractTable: {records: []},
                approvalOperating: "", //审批结果
                pageInfo: {},
                revokeFrom: {},
                revokeHandleInfo: {
                    termsPayment: "",
                    refundBankCode: "",
                    payer: "",
                    refundAccount: "",
                    branchBank: "",
                    bankNo: "",
                    receiptId: "",
                    treatmentMethod: "",
                    id: ""
                },
                modalTitle: "",
                detailTitle: "",
                pageParams: {},
                contractNo: null,
                creditStatus: null,
                paymentStatus: null,
                show: false,
                customerInfo: {},
                type: null,
                Obj: {},
                caseNo: "",
                id: null,
                defaultContractData: {},
                contractForm: {},
                contractFormValidate: {},
                dicttArr: ["bankCode", "processStatus", "businessSource", "PersonRoleType", "contractStatus", "channelType",
                    "creditStatus", "receiptSourceType", "feeType"],
                dataDict: {},
                isSP: true,
            };
        },
        computed: {
            ...mapGetters({userInfo: "userInfo", roleType: "roles"}),
            isFinance: function () {
                return FinanceType(this.roleType)
            },
        },
        created() {
            this.getParams();
            this.isSP = channelType(this.$route.meta.params);
        },
        mounted() {
            this.init();

        },
        watch: {},
        methods: {
            ...mapActions(["getDictData"]),
            init() {
                if (this.pageParams.caseNo) {
                    this.getManageRevokeHandle();
                } else {
                    this.revokeHandleInfo.arriveAmt = this.pageParams.arriveAmt;
                    this.revokeHandleInfo.termsPayment = "现金";
                    this.getHandle();
                }
                if (this.$refs.baseInfo) {
                    this.$refs.baseInfo.formData = Object.assign(
                        {},
                        this.$refs.baseInfo.formData,
                        this.pageInfo
                    );
                }
                // 数据字典
                this.getDictDataList();
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    this.dataDict = res;
                    this.dataDict.treatmentMethodDict = [{title: "收入", value: 'INCOME'}, {
                        title: '退款',
                        value: 'REFUND'
                    }];
                });
            },
            // 获取初始数据
            getParams() {
                const params = this.afs.getPageParams(this);
                this.pageParams = params;
                this.params = params;
                this.type = params.type;
                this.readOnly = params.type === "read" ? true : false;
                this.contractNo = params.contractNo;
                this.receiptId = params.receiptId;
                this.isApprove =  params.processStatus === "underReview";
                if (params.bizDataId === undefined) {
                    this.id = params.id;
                } else {
                    this.id = params.bizDataId;
                }
                this.Obj = {
                    isFinance: this.isFinance,
                    roleType: this.isSP,
                    arriveAmt: params.arriveAmt,
                    caseNo: params.caseNo,
                    contractNo: params.contractNo,
                    processStatus: params.processStatus,
                    receiptId: params.receiptId
                }
            },
            formData() {
                this.contractForm.applyNo = this.$refs.contractInfo.formData.applyNo;
                this.contractForm.contractNo = this.$refs.contractInfo.formData.contractNo;
                this.contractForm.custName = this.$refs.contractInfo.formData.basicCustBaseInfo.custName;
                this.contractForm.contractStatus = this.$refs.contractInfo.formData.contractStatus;
                this.contractForm.channelFullName = this.$refs.contractInfo.formData.channelFullName;
                this.contractForm.channelType = this.$refs.contractInfo.formData.channelType;
                this.contractForm.phone = this.$refs.contractInfo.formData.basicCustBaseInfo.phone;
                this.contractForm.certNo = this.$refs.contractInfo.formData.basicCustBaseInfo.certNo;
                this.contractForm.sourceType = this.$refs.RevokeFrom.formData.sourceType;
                this.contractForm.succeedTime = this.$refs.RevokeFrom.formData.succeedTime;
                this.contractForm.arriveAmt = this.$refs.RevokeFrom.formData.arriveAmt;
                this.contractForm.receiptId = this.receiptId;
                if (this.isSP) {
                    this.contractForm.department = this.$refs.contractInfo.channelFullName;
                } else {
                    this.contractForm.department = this.userInfo.userRealName;
                }
                this.contractForm.id = this.$refs.revokeHandleInfo.formData.id;
                this.contractForm.refundBankCode = this.$refs.revokeHandleInfo.formData.refundBankCode;
                this.contractForm.payer = this.$refs.revokeHandleInfo.formData.payer;
                this.contractForm.refundAccount = this.$refs.revokeHandleInfo.formData.refundAccount;
                this.contractForm.branchBank = this.$refs.revokeHandleInfo.formData.branchBank;
                this.contractForm.bankNo = this.$refs.revokeHandleInfo.formData.bankNo;
                this.contractForm.termsPayment = this.$refs.revokeHandleInfo.formData.termsPayment;
                this.contractForm.treatmentMethod = this.$refs.revokeHandleInfo.formData.treatmentMethod;
                if (this.$refs.contractCurrentUser !== undefined) {
                    this.contractForm.reasonType = this.$refs.contractCurrentUser.formData.reasonType;
                    this.contractForm.approvalOpinion = this.$refs.contractCurrentUser.formData.approvalOpinion;
                }
                this.contractForm.businessSource = businessSource(this.roleType);
            },
            submit() {
                this.$Modal.confirm({
                    title: "确认提交？",
                    onOk: () => {
                        if (this.$refs.revokeHandleInfo) {
                            this.showSpin();
                            this.$refs.revokeHandleInfo.formValidate("submitDataFn");
                        }
                    }
                });
            },
            submitDataFn() {
                this.formData();
                revokedStartProcess(this.contractForm)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            },
            // 审核等按钮
            approve(name) {
                if (!name) return;
                let t, val;
                switch (name) {
                    case "pass":
                        t = "审核通过";
                        val = "suggestCheckF";
                        break;
                    case "return":
                        t = "退回";
                        val = "sendBack";
                        break;
                }
                this.$Modal.confirm({
                    title: `确认${t}？`,
                    onOk: () => {
                        if (this.$refs.revokeHandleInfo) {
                            this.showSpin();
                            this.approvalOperating = val;
                            // 验证 business 组件数据 给下一个方法
                            this.$refs.revokeHandleInfo.formValidate("childBusinessForm");
                        }
                    }
                });
            },
            // 审批 请求
            // Business 组件数据
            childBusinessForm(data) {
                if (!data) return;
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate(
                    "childUserForm",
                    data
                );
            },
            // contractCurrentUser 组件数据
            childUserForm(data) {
                if (!data) return;
                this.formData();
                this.contractForm.approvalOperating = this.approvalOperating;
                revokeSubmitProcess(this.contractForm)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            },

            showSpin() {
                this.$emit("showSpin");
            },
            closeSpin() {
                this.$emit("closeSpin");
            },
            back() {
                //关闭页面
                this.afs.closeTab(this);
            },

            getManageRevokeHandle() {
                getManageRevokeHandle(this.pageParams.caseNo)
                    .then(res => {
                        this.loading = false;
                        if (res.code === "0000" && res.data) {
                            this.pageInfo = res.data;
                            this.pageInfo.applyDate = res.data.startTime;
                            this.pageInfo.endDate = res.data.endTime;
                            this.pageInfo.applyBy = res.data.createBy;
                            this.revokeHandleInfo = res.data;
                            this.receiptId=res.data.receiptId;
                            this.getHandle()
                        } else {
                            this.$Message.error("Fail!");
                        }
                    })
                    .catch(e => {
                        console.error(e);
                    });
            },
            getHandle() {
                getReceiptRecord(this.receiptId)
                    .then(res => {
                        this.loading = false;
                        if (res.code == "0000" && res.data) {
                            this.revokeFrom = res.data;
                        } else {
                            this.$Message.error("Fail!");
                        }
                    })
                    .catch(e => {
                        console.error(e);
                    });
            },

        }
    };
</script>
